package com.piz.web.user;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.piz.model.City;
import com.piz.service.common.UnexpectedServiceException;
import com.piz.service.userservice.UserService;

@Controller
public class CityStateController {
	// Obtain a suitable logger.
	  private static Log logger = LogFactory
	      .getLog(CityStateController.class);
	  
	@Autowired
	UserService userService;
	
	@RequestMapping(value = "cities",headers = "Accept=*/*", method = RequestMethod.GET)
	public @ResponseBody
	Set<City> getCitiesInState(@RequestParam(value = "stateId", defaultValue = "") Long state)
	{
		List<City> cities = null;
		try {
			cities = userService.findCitiesInState(state);
		} catch (UnexpectedServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Set<City> cityNames = new HashSet<City>();
		for(City city : cities)
		{
			cityNames.add(city);
		}
		return cityNames;
		
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
